<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;

use DB;

use Session;

class Typecontroller extends Controller
{
    public function index(Request $request){
    	$data=DB::table('goods_cate')
    	-> select('*',DB::raw("CONCAT(cate_path,id) AS sortPath"))
    	-> where('cate_name','like','%'.$request -> input('keyword').'%')
    	->orderby('sortPath')
    	-> paginate($request -> input('pages',5));

    	 foreach($data as $k =>$v){
    	 	// 计算 cate_path 的逗号个数
    	 	$num =substr_count($v -> cate_path,',');

    	 	$data[$k] -> cate_name =str_repeat('&nbsp; &nbsp;', $num).'|--'.$v -> cate_name;
    	 }


    	return view('admin/type/index',['data' => $data,'request' => $request -> all()]);
    }

    public function addMain(){
    	return view('admin/type/addMain');
    }


    public function insertMain(Request $request){
        // var_dump($request -> all());
        // var_dump($request);
        $attr['cate_name']= $request -> cate_name;
        $data=$request -> except('_token','cate_name');
        // dd($data);
        $this->validate($request, [
            'cate_name' => 'required|max:255',
        ],[
            'cate_name.required' => '商品类型名称不能为空',
            'cate_name.max' => '商品类型名称不能多于255位',
        ]);
        $attr['cate_pid'] = '0';
        $attr['cate_path'] ='0,';
        // var_dump($attr);
        // dd($data);
        $str = '';

        foreach ($data as $k => $v) {
            $res = DB::table('goods_attr') -> where(['attr_name' => $v]) -> first();
            if($res){
                // dd($res);
                $attrID = $res ->id;
            }else{
               $attrID = DB::table('goods_attr') -> insertGetId(['attr_name' => $v]); 
            }
            

            $str .= $attrID.',';
        }
        

        $goods_attrs_id = trim($str,',');

        // dd($goods_attrs_id);

        $attr['goods_attrs_id'] = $goods_attrs_id;

        // dd($attr);
        $res=DB::table('goods_cate') -> insert($attr);

        if ($res) {
            return redirect('/admin/type');
        }else{
            return back()->with(['info' => '数据添加失败']);
        }
    }

    public function add($id){
    	$data=DB::table('goods_cate') -> where('id' , $id) ->first();
    	$goods_attrs_id = $data -> goods_attrs_id;
    	// dd($goods_attrs_id);
    	$attrs= explode(',', $goods_attrs_id);
    	
    	$attrs_arr=array();
    	foreach ($attrs as $k => $v) {
    		$attr_name=DB::table('goods_attr') -> where('id' , $v) ->first();
    		$attrs_arr["$v"] = $attr_name -> attr_name ;
    	}
    	// dd($attrs_arr);
		return view('admin/type/add',['data' => $data , 'attrs_arr' => $attrs_arr]);
    }

    public function insert(Request $request){
    	// dd($request -> all());
    	// var_dump($request);
    	// 判断商品名称是否符合要求
    	$this->validate($request, [
		    'cate_name' => 'required|max:255',
		],[
			'cate_name.required' => '商品类型名称不能为空',
			'cate_name.max' => '商品类型名称不能多于255位',
		]);

    	$alldata = $request -> all();
    	// dd($alldata);
    	$attr['cate_name']= $request -> cate_name;
    	$attr['cate_pid'] =$alldata['id'];
    	$attr['cate_path']= $alldata['path'].$attr['cate_pid'].',';
    	// dd($cate_pid);
    	$data=$request -> except('_token','cate_name','pid','path','id');
    	// dd($data);
    	// var_dump($attr);
    	// dd($data);
    	$str = '';

    	foreach ($data as $k => $v) {

    		$res = DB::table('goods_attr') ->where('id',$v) ->first();

    		if($res){
    			$str .=$v.',';
    		}else{
    			$attrID = DB::table('goods_attr') -> insertGetId(['attr_name' => $v]);

    			$str .= $attrID.',';
    		}

    		
    	}
    	
		$goods_attrs_id = trim($str,',');
    	
    	// dd($goods_attrs_id);

    	$attr['goods_attrs_id'] = $goods_attrs_id;

    	// dd($attr);
    	$res=DB::table('goods_cate') -> insert($attr);

    	if ($res) {
    		return redirect('/admin/type');
    	}else{
    		return back()->with(['info' => '数据添加失败']);
    	}
    }

    

    public function edit($id){

    	$data=DB::table('goods_cate') -> where('id' , $id) ->first();

    	$goods_attrs_id = $data -> goods_attrs_id;
    	// dd($goods_attrs_id);
        // 去除两侧的逗号
        $goods_attrs_id = trim($goods_attrs_id,',');
        
    	$attrs= explode(',', $goods_attrs_id);
    	
    	$attrs_arr=array();
    	foreach ($attrs as $k => $v) {
    		$attr_name = DB::table('goods_attr') -> where('id' , $v) ->first();
            // dd($attr_name);
    		$attrs_arr[$v] = $attr_name -> attr_name ;
            // dd($attrs_arr);
    	}
    	// dd($data);
    	return view('admin.type.edit',['data' => $data , 'attrs_arr' => $attrs_arr]);
    }

    public function doedit(Request $request){
    	// dd($request -> all());
    	// var_dump($request);
    	// 判断商品名称是否符合要求
    	$this->validate($request, [
		    'cate_name' => 'required|max:255',
		],[
			'cate_name.required' => '商品类型名称不能为空',
			'cate_name.max' => '商品类型名称不能多于255位',
		]);

    	$alldata = $request -> all();
    	// dd($alldata);
    	$id =$alldata['id'];
    	$cate_name = $alldata['cate_name'];
    	// dd($cate_pid);
    	$data=$request -> except('_token','cate_name','pid','path','id');
    	// dd($data);
    	// var_dump($attr);
    	// dd($data);
    	$str = '';

    	foreach ($data as $k => $v) {

    		$res = DB::table('goods_attr') ->where('id',$v) ->first();

    		if($res){
    			$str .=$v.',';
    		}else{
    			$attrID = DB::table('goods_attr') -> insertGetId(['attr_name' => $v]);

    			$str .= $attrID.',';
    		}

    		
    	}
    	
		$goods_attrs_id = trim($str,',');
    	
    	// dd($goods_attrs_id);

    	$attr['goods_attrs_id'] = $goods_attrs_id;

    	// dd($attr);
        // dd($id);
        // dd($cate_name);
    	$res=DB::table('goods_cate') ->where('id',$id) -> update(['goods_attrs_id' => $goods_attrs_id , 'cate_name' =>$cate_name ]);
        // dd($res);
    	if ($res) {
    		return redirect('/admin/type');
    	}else{
    		return back()->with(['info' => '数据添加失败']);
    	}
    }

    public function delete($id){
    	$res = DB::table('goods_cate') -> where('id',$id)->first();
    	// dd($res);
    	$resson=DB::table('goods_cate') -> where('cate_pid',$res -> id)->first();
    	// dd($resson);
    	if($resson){
    		return back() -> with(['info' => '该分类下有子分类,无法删除']);
    	}else{
    		$res=DB::table('goods_cate') -> where('id',$id)->delete();

    		return redirect('/admin/type') -> with(['info' => '删除成功']);
    	}
    	
    }
 
}
